package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 会员SERVICE实现类
 */
@Service(interfaceClass = MemberService.class)
@Slf4j
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    @Transactional
    @Override
    public Member addIfNotExist(Member member) {
        log.info("[会员-添加]data:{}",member);
        Member pojo = memberDao.selectPhoneNumber(member.getPhoneNumber());
        if(null != pojo) {
            //会员已添加，直接返回
            return pojo;
        }
        //调用DAO插入数据
        memberDao.insert(member);
        return memberDao.selectById(member.getId());
    }

    @Override
    public Member findById(Integer id) {

        return memberDao.selectById(id);
    }

    @Override
    public List<Integer> countByMonth(List<String> months) {
        List<Integer> resultLIst = new ArrayList<>();
        for (String month : months) {
            String endDate = month+".31";
            Long count = memberDao.countByRegTimeBefore(endDate);
            resultLIst.add(count.intValue());
        }
        return resultLIst;
    }

    @Override
    public List<Integer> countByTwoMonth(List<String> months) {
        //判断传来的日期进行比较
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        log.info("months:{}", months);
        ArrayList<Integer> list = new ArrayList<>();
        log.info("list:{}", list);
        for (int i = 0; i < months.size(); i++) {
            String oneDate = months.get(0) + "-01";
            String endDate = months.get(i) + "-31";
            log.info("oneDate:{},twoDate:{}", oneDate, endDate);
            try {
                Date date1 = sf.parse(oneDate);
                Date date2 = sf.parse(endDate);

                /**
                 * 获取每个月的count
                 */
                Long count = memberDao.countByRegTimeRange(date1, date2);
                list.add(count.intValue());

            } catch (ParseException e1) {
                log.error("",e1);
            }
        }
        return list;
    }


    @Override
    public List<Map<String,Object>> memberRange() {
        //数据库查询生日，存放在list集合
        List<String> birthday = memberDao.selectbirthday();
        log.info("[birthday]-->{}",birthday);
        //定义四个变量，用于表示不同年龄阶段的人数
        int count1=0;
        int count2=0;
        int count3=0;
        int count4=0;
        //日期格式化
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dateNow = new Date();

        //遍历生日集合
        for (String s : birthday) {
            log.info("[birthday]---------------{}", s);
            //格式转换
            if (null != s) {

                Date date = null;//date = Sun Nov 24 00:00:00 CST 2019
                try {
                    date = sdf.parse(s);
                } catch (ParseException e) {
                    log.error("",e);
                }

                int age = 0;
            Calendar born = Calendar.getInstance();//获取出生日期
            Calendar now = Calendar.getInstance();//获取当前日期
                if (date != null) {
                now.setTime(dateNow);
                born.setTime(date);
                if (born.after(now)) {
                    throw new IllegalArgumentException("年龄不能超过当前日期");
                }
                //计算有多少年
                age = now.get(Calendar.YEAR) - born.get(Calendar.YEAR);
                int nowDayOfYear = now.get(Calendar.DAY_OF_YEAR);
                int bornDayOfYear = born.get(Calendar.DAY_OF_YEAR);
                System.out.println("nowDayOfYear:" + nowDayOfYear + " bornDayOfYear:" + bornDayOfYear);
                if (nowDayOfYear < bornDayOfYear) {
                    age -= 1;
                }
            }
            if (age <= 20) {
                count1++;
            }
            if (age > 20 & age <= 40) {
                count2++;
            }
            if (age > 40 & age <= 60) {
                count3++;
            }
            if (age > 60) {
                count4++;
            }
        }
        }

        Map<String,Object> map1= new HashMap<>();
        Map<String,Object> map2= new HashMap<>();
        Map<String,Object> map3= new HashMap<>();
        Map<String,Object> map4= new HashMap<>();

        map1.put("name","0-20岁");
        map1.put("value",count1);
        map2.put("name","20-40岁");
        map2.put("value",count2);
        map3.put("name","40-60岁");
        map3.put("value",count3);
        map4.put("name","60岁以上");
        map4.put("value",count4);
        List<Map<String,Object>> list = new ArrayList<>();
        list.add(map1);
        list.add(map2);
        list.add(map3);
        list.add(map4);
        log.info("[list]{}",list);
        return list;//[{"name":"0-20","value":1},{"name":"20-40","value":2}.....]
    }

    @Override
    public List<Map<String,Object>> getMemberGenderReport() {

        return memberDao.getMemberGenderReport();
    }
}
